<template>
  <div class="app-container">
    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="moduleInfo.readonly == 'N'">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['run:'+moduleInfo.code+':add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5" v-if="moduleInfo.readonly == 'N' && moduleInfo.importable == 'Y'">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['run:'+moduleInfo.code+':import']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5" v-if="moduleInfo.exportable == 'Y'">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['run:'+moduleInfo.code+':export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table
        v-if="refreshTable"
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
        row-key="id"
        :default-expand-all="isExpandAll"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="(f, i) in fieldList">
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-if="f.list == 'Y' && f.type == 'checkbox'"
        >
          <template slot-scope="scope">
            {{ dictLabel(f.code, scope.row[f.code])}}
          </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.type == 'radio'"
        >
          <template slot-scope="scope">
            {{ dictLabel(f.code, scope.row[f.code])}}
          </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.form == 'tree'"
        />
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.form == 'select'"
        />
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.form == 'date'"
        >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row[f.code], '{y}-{m}-{d}') }}</span>
        </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.form == 'datetime'"
        >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row[f.code]) }}</span>
        </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y'"
        />
      </template>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button v-if="moduleInfo.readonly == 'N'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['run:'+moduleInfo.code+':edit']"
            >修改</el-button
          >
          <el-button v-if="moduleInfo.readonly == 'N'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['run:'+moduleInfo.code+':remove']"
            >删除</el-button
          >
          <el-button v-if="moduleInfo.readonly == 'Y'"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <form-dialog v-if="fieldList"
      @submit="submitForm"
      @cancel="cancel"
      :data="formData"
      :action="formAction"
      :title="formTitle"
      :fields="fieldList"
    ></form-dialog>

    <!-- 导入 -->
    <import-dialog v-if="moduleInfo"
      :open="upload"
      :model="moduleInfo['api_model']"
      @cancel="importCancel"
      @success="importSuccess"
    />
  </div>
</template>

<script>
import { apiGet, apiDs } from "@/api/openapi";
import ListMixin from "./list"
import ImportDialog from "@/components/ImportDialog";
import FormDialog from "./form"
import SearchForm from "./search"

export default {
  name: "RunTree",
  components: {
    ImportDialog, FormDialog, SearchForm
  },
  dicts: [],
  mixins: [ListMixin],
    data() {
    return {
    refreshTable:true,
    isExpandAll: true
    };
  },
  methods: {    
    /** 查询资产分类列表 */
    getList() {
      this.loading = true;
      let params = {...this.queryParams};
      params['@pager'] = {limit:1000, sort:"sorts ASC"};

      if (this.moduleInfo['api_ds']) {
          apiDs(this.moduleInfo['api_ds'], params).then(response => {
            this.dataList = this.handleTree(response.data, "id", "pid");
            this.loading = false;
          });
      } else {
          params["@table"] = this.moduleInfo['api_model'];
          apiGet(params).then(response => {
            this.dataList = this.handleTree(response.data.rows, "id", "pid");
            this.loading = false;
          });
      }
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    }
  }
};
</script>

<!--
列表字段的处理逻辑如下：
1、按排序展示字段，可设置列表不展示，具体内容按类型进行格式化
2、普通内容原样展示
3、日期和日期时间自动格式化为 yyyy-MM-dd和yyyy-MM-dd hh:mm:ss
4、支持图片预览
5、支持字典转义
-->